package com.loveprogrammer.eternity.factory;

import com.alibaba.fastjson.JSONArray;
import com.loveprogrammer.eternity.enums.SkillQualityEnum;
import com.loveprogrammer.eternity.factory.template.MonsterTemplate;
import com.loveprogrammer.eternity.factory.template.SkillTemplate;
import com.loveprogrammer.eternity.skill.Skill;
import com.loveprogrammer.eternity.skill.support.skills.AbstractSkill;
import com.loveprogrammer.eternity.utils.BeanCopyUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0.0
 * @description: 技能工厂
 * @author: eric
 * @date: 2022-08-18 15:42
 **/
public class SkillFactory {

    private static List<SkillTemplate> skillTemplates;
    private static Map<Integer, AbstractSkill> skillMap = new HashMap<>();

    static {
        try {
            // 读取配置文件，然后加载到weaponTemplates中
            ClassLoader classLoader = EquipmentFactory.class.getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream("skill.json");

            assert inputStream != null;
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);

            skillTemplates = JSONArray.parseArray(responseStrBuilder.toString(), SkillTemplate.class);

            // 第二步，要通过反射生成技能map
            for (SkillTemplate skillTemplate : skillTemplates) {
                try {
                    String className = skillTemplate.getClassName();
                    Class<?> clazz = Class.forName(className);
                    Constructor<?>[] constructors = clazz.getConstructors();
                    // 遍历所有的构造方法
                    for (Constructor<?> constructor : constructors) {
                        if(skillTemplate.getDodged() != null && constructor.getParameterTypes().length == 8){
                            Object[] args = new Object[]{skillTemplate.getId(),skillTemplate.getInterval(),1
                                    ,skillTemplate.getOrder(),skillTemplate.getMax(),skillTemplate.getName()
                                    ,skillTemplate.getDesc(),skillTemplate.getDodged()};
                            AbstractSkill instance = (AbstractSkill) clazz.getDeclaredConstructor(constructor.getParameterTypes()).newInstance(args);
                            skillMap.put(instance.getId(),instance);
                            break;
                        }
                        // 找到字段长度为7的
                        else if (constructor.getParameterTypes().length == 7) {
                            Object[] args = new Object[]{skillTemplate.getId(),skillTemplate.getInterval(),1
                                    ,skillTemplate.getOrder(),skillTemplate.getMax(),skillTemplate.getName(),skillTemplate.getDesc()};
                            AbstractSkill instance = (AbstractSkill) clazz.getDeclaredConstructor(constructor.getParameterTypes()).newInstance(args);
                            skillMap.put(instance.getId(),instance);
                            break;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Skill getNewSkillById(int id, SkillQualityEnum qualityEnum) throws IOException, ClassNotFoundException {
        AbstractSkill skill = skillMap.get(id);
        if(skill != null){
            AbstractSkill copy = BeanCopyUtils.deepCopy(skill);
            // 扩展 嘲讽类型的首次冷却时间归0
            if(copy.getId() == 11 || copy.getId() == 40){
                copy.setRestInterval(0);
            }
            copy.setQuality(qualityEnum);
            return copy;
        }
        return null;
    }

    public static Skill getNewSkillById(String id, SkillQualityEnum qualityEnum) throws IOException, ClassNotFoundException {
        if(id == null || "".equals(id)){
            return null;
        }
        return getNewSkillById(Integer.parseInt(id),qualityEnum);
    }

    public static Skill getNewSkillByIdAndMonsterId(String skillId,int monsterId) throws IOException, ClassNotFoundException {
        if(skillId == null || "".equals(skillId)){
            return null;
        }
        AbstractSkill skill = skillMap.get(Integer.parseInt(skillId));
        if(skill != null){
            AbstractSkill copy = BeanCopyUtils.deepCopy(skill);
            // 扩展 嘲讽类型的首次冷却时间归0
            if(copy.getId() == 11){
                copy.setRestInterval(0);
            }
            MonsterTemplate monsterTemplate = MonsterFactory.getById(monsterId);
            String skills = monsterTemplate.getSkills();
            String[] skillArray = skills.split(",");
            String skillQuality = monsterTemplate.getSkillQuality();
            String[] skillQualityArray = skillQuality.split(",");
            int index = 0;
            for (int i = 0; i < skillArray.length; i++) {
                if(skillArray[i].equals(skillId)){
                    index = i;
                }
            }
            copy.setQuality(SkillQualityEnum.getByCode(skillQualityArray[index]));
            // 其他扩展
            return copy;
        }
        return null;
    }


    public static void main(String[] args) {
        for (SkillTemplate skillTemplate : skillTemplates) {
            System.out.println(skillTemplate.toString());
        }

        System.out.println(skillMap.size());
        System.out.println("end");
    }
}
